package io.renren.modules.demo.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.renren.modules.demo.entity.GoodsEntity;
import io.renren.modules.demo.service.GoodsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import org.springframework.web.multipart.MultipartFile;


/**
 * 商品管理
 * 
 * @author liupengfei
 * @email phineasliu@163.com
 * @date 2018-07-04 12:54:19
 */
@RestController
@RequestMapping("demo/goods")
public class GoodsController {
	@Autowired
	private GoodsService goodsService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("demo:goods:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<GoodsEntity> goodsList = goodsService.queryList(query);
		int total = goodsService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(goodsList, total, query.getLimit(), query.getCurrPage());
		
		return R.ok().put("page", pageUtil);
	}

    /**
     * echart图标
     */
    @RequestMapping("/getChartList")
    @RequiresPermissions("demo:goods:list")
    public R getChartList(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        List<GoodsEntity> result = goodsService.queryList(query);
//        int total = goodsService.queryTotal(query);

//        PageUtils pageUtil = new PageUtils(goodsList, total, query.getLimit(), query.getCurrPage());
        Map<String, List<?>> map = new HashMap();
        if ((result != null) && (result.size() > 0))
        {
            //x轴名称
            List<String> xAxisData = new ArrayList();
            //价格
            List<Double> seriesPriceData = new ArrayList();
            //数量
            List<Double> seriesNumData = new ArrayList();
            for (GoodsEntity bean : result)
            {
                xAxisData.add(bean.getName());
                seriesPriceData.add(bean.getPrice().doubleValue());
                seriesNumData.add(Double.parseDouble(bean.getNum().toString()));
            }
            map.put("xAxisData",xAxisData);
            map.put("seriesPriceData",seriesPriceData);
            map.put("seriesNumData",seriesNumData);
        }
        return R.ok().put("data", map);

//        return R.ok().put("page", pageUtil);
    }
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{goodsId}")
	@RequiresPermissions("demo:goods:info")
	public R info(@PathVariable("goodsId") Long goodsId){
		GoodsEntity goods = goodsService.queryObject(goodsId);
		
		return R.ok().put("goods", goods);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("demo:goods:save")
	public R save(@RequestBody GoodsEntity goods){
		goodsService.save(goods);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("demo:goods:update")
	public R update(@RequestBody GoodsEntity goods){
		goodsService.update(goods);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("demo:goods:delete")
	public R delete(@RequestBody Long[] goodsIds){
		goodsService.deleteBatch(goodsIds);
		
		return R.ok();
	}


	/**
	 * 导入excel
	 */
	@RequestMapping("/exportExcel")
	public R exportExcel(@RequestParam("file") MultipartFile file){
        boolean a = false;
        String fileName = file.getOriginalFilename();
        System.err.println(fileName);
        return goodsService.exportExcel(file);
	}
}
